package wxy.studentmanage.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import wxy.studentmanage.bean.Student;
import wxy.studentmanage.bean.Users;
import wxy.studentmanage.service.StudService;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/stud")
public class StudController {
    @Autowired
    @Qualifier("StudServiceImpl")
    private StudService studService;
    @RequestMapping("/allStud")
    public String allStud(Model model){
        List<Student> slist = studService.queryAllUser();
        model.addAttribute("slist", slist);
        System.out.println(slist);
        System.out.println(slist);
        return "allStudent";
    }
    //新增用户页面
    @RequestMapping("/toAddStud")
    public String toAddPaper() {
        return "addStudent";
    }
    //添加Student
    @RequestMapping("/addStud")
    public String addUser(Student student){
        System.out.println(student);
        studService.addStudent(student);
        return "redirect:/stud/allStud";
    }
    //删除User
    @RequestMapping("/del/{userId}")
    public String deleteStud(@PathVariable("userId") int wxy_id) {
        System.out.println(wxy_id);
        int i = studService.deleteStudById(wxy_id);
        System.out.println(i);
        return "redirect:/stud/allStud";
    }
    //更改查询
    @RequestMapping("/toUpdateStud/{userId}")
    public String toUpdateUser(@PathVariable("userId") int wxy_id,Model model){
        System.out.println(wxy_id);
        Student student = studService.queryStudById(wxy_id);
        System.out.println(student);
        model.addAttribute("student",student);
        return "updateStudent";
    }
    //更改更改
    @RequestMapping("/updateStud")
    public String updateBook(Model model,Student student){
        System.out.println(student);
        studService.updateStud(student);
        Student student1 = studService.queryStudById(student.getWxy_id());
        System.out.println(student1);
        return "redirect:/stud/allStud";
    }
    //查询
    @RequestMapping("/queryBook")
    public String queryBook(String wxy_username,Model model){
        System.out.println(wxy_username);
        Student student =  studService.queryName(wxy_username);
        System.err.println(student);
        List<Student> list = new ArrayList<>();
        list.add(student);
        if( student == null){
            list = studService.queryAllUser();
            model.addAttribute("error","未查到");
        }
        model.addAttribute("slist",list);
        return "allStudent";
    }

    @RequestMapping("/login1")
    public String login1(){
        return "login1";
    }
    @RequestMapping("/allSth")
    public String allSth(@RequestParam String wxy_username, @RequestParam String wxy_password, Model model){
        System.out.println(wxy_username+"*******"+wxy_password);
        Student student = studService.login(wxy_username,wxy_password);
        System.out.println(student);
        if(student == null){
            return "error1";
        }
        model.addAttribute("student",student);
        return "allSth";

//
//        System.out.println(student);
//        List<Student> list = new ArrayList<>();
//        list.add(student);
//        if (student != null){
//            model.addAttribute("slist",list);
//            return "allSth";
//        }
//        return "error";
    }
    @RequestMapping("/zhuXiao")
    public String zhuXiao(){
        return "index";
    }
    @RequestMapping("/fenye1")
    public String fenye1(){
        return "redirect:/stud/allStud";
    }
    @RequestMapping("/fenye2")
    public String fenye2(){
        return "allStudent1";
    }

}
